<template>
  <!-- <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
     <a-divider orientation="left">
       <b>{{ formTitle }}</b>
     </a-divider> -->
  <a-modal :visible="open" :width="750" :title="formTitle" @cancel="onClose">
    <a-form-model ref="form" :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }" :model="form" :rules="rules">
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="车型" prop="info">
            <a-input v-model="form.info" placeholder="请输入车型" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="单价（元/公里）" prop="price">
            <a-input-number v-model="form.price" style="width: 100%;" placeholder="请输入单价" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="状态" prop="status">
            <a-radio-group v-model="form.status" button-style="solid">
              <a-radio-button v-for="(d, index) in statusOptions" :key="index" :value="d.value">{{ d.label
                }}</a-radio-button>
            </a-radio-group>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="24">
          <a-form-model-item :label-col="{ span: 4 }" :wrapper-col="{ span: 19 }" label="备注" prop="remark">
            <a-input type="textarea" aria-rowcount="5" v-model="form.remark" placeholder="请输入备注" />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
    <template slot="footer">
      <a-space>
        <a-button type="primary" :loading="submitLoading" @click="submitForm">
          保存
        </a-button>
        <a-button type="dashed" @click="cancel">
          取消
        </a-button>
      </a-space>
    </template>
  </a-modal>
  <!-- </a-drawer> -->
</template>

<script>
import { getInfo, addInfo, updateInfo } from '@/api/vehicle/cartypeinfo'

export default {
  name: 'CreateForm',
  props: {
    statusOptions: {
      type: Array,
      required: true
    },
  },
  components: {
  },
  data() {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,

        info: null,

        price: null,

        status: '0',

        remark: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ],
        info: [
          { required: true, message: '车型信息不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  filters: {
  },
  created() {
  },
  computed: {
  },
  watch: {
  },
  mounted() {
  },
  methods: {
    onClose() {
      this.open = false
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.formType = 1
      this.form = {
        id: null,

        info: null,

        price: null,

        status: '0',

        remark: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        delFlag: null

      }
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate(row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateInfo(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addInfo(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
